const express = require("express")
const router = express.Router()
const adminServ = require("../../services/adminservice")
const {
    asyncHandler,getResult
} = require("../getSendResult");
const a = require("../../util/crypt");
const jm = require("../../util/crypt")
router.post("/login", asyncHandler(async (req, res, next) => {
    // login用户登录
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd)
    if (result) {
        // 登陆成功
        // 针对其他终端
        let value = result.id;
        // value = a.encrypt(value.toString()) //使用自己写的加密方法
        res.cookie("token", value, {
            path: "/",
            domain: "localhost",
            maxAge: 7 * 24 * 3600 * 1000, //毫秒
        })
        res.header("authorization", value);
    }
    return result
}))
router.post("/register",asyncHandler(async(req,res,next)=>{
    return await adminServ.addAdmin(req.body)
}))
router.post("/find",asyncHandler(async(req,res,next)=>{
   if(req.body.loginId){
    return await adminServ.getAdminId(req.body.loginId)
  }else{
  return  getResult("no token！")
  }
}))

router.get("/updateadmin",asyncHandler(async(req,res,next)=>{
    const id=req.query.id
    const dzg=req.query.dzg
    return await adminServ.updateadmin(id,dzg)
}))
module.exports = router